<?php
/**
 * 导出商家的 用户列表
 *
 * Author: lyun
 * Date: 2018/5/7
 * Created by Panxsoft.
 */

namespace App\Services\ExcelExporters;

use App\Models\OpenWechat\WechatUser;
use App\Services\Utils\TimeHelper;
use Illuminate\Database\Eloquent\Collection;
use Maatwebsite\Excel\Classes\LaravelExcelWorksheet;
use Maatwebsite\Excel\Excel;
use Maatwebsite\Excel\Writers\LaravelExcelWriter;

class UserExporter
{
    /** @const VIEW - 模板文件的名字 */
    const VIEW = 'excel.user';

    /** @const EXT - 导出的格式 */
    const EXT = 'xlsx';

    /** @const SHEET - excel分页的名字 */
    private $sheet = '商家用户';

    const SEX_MAPS = [
        'zh-CN' => ['未知', '男', '女'],
        'en'    => ['unknown', 'male', 'female'],
        'zh-HK' => ['未知', '男', '女']
    ];

    /**
     * @param Collection $data
     * @return mixed
     */
    public function transform(Collection $data)
    {
        $language = app()->getLocale();

        $data = $data->transform(function(WechatUser $wechat_user) use($language){
            $wechat_user->sex_name = self::SEX_MAPS[$language][$wechat_user->sex];
            $wechat_user->filter_nick_name = $this->filterEmoJi($wechat_user->nickname);

            return $wechat_user;
        });

        return $data->all();
    }

    /**
     * 返回 excel 文件名
     */
    public function filename()
    {
        return $this->sheet . TimeHelper::toFileSuffix();
    }

    /**
     * 直接导出excel
     *
     * @param Collection $data
     * @param $sheet_name
     * @throws \Maatwebsite\Excel\Exceptions\LaravelExcelException
     */
    public function export(Collection $data, $sheet_name)
    {
        $this->sheet = $sheet_name;

        $data = $this->transform($data);

        /** @var Excel $excel */
        $excel = app('excel');

        $excel->create($this->filename(), function (LaravelExcelWriter $excel) use ($data) {
            $excel->sheet($this->sheet, function (LaravelExcelWorksheet $sheet) use ($data) {
                $sheet->setColumnFormat([
                    'D' => '@'
                ]);

                $sheet_name = $this->sheet;

                $sheet->loadView(self::VIEW, compact('data', 'sheet_name'));
            });
        })->export(self::EXT);
    }


    /**
     * 过滤掉emoji表情
     *
     * @param $text
     * @param string $replaceTo
     * @return null|string|string[]
     */
    private function filterEmoJi($text, $replaceTo = '?')
    {
        $clean_text = "";
        // Match Emoticons
        $regexEmoticons = '/[\x{1F600}-\x{1F64F}]/u';
        $clean_text = preg_replace($regexEmoticons, $replaceTo, $text);
        // Match Miscellaneous Symbols and Pictographs
        $regexSymbols = '/[\x{1F300}-\x{1F5FF}]/u';
        $clean_text = preg_replace($regexSymbols, $replaceTo, $clean_text);
        // Match Transport And Map Symbols
        $regexTransport = '/[\x{1F680}-\x{1F6FF}]/u';
        $clean_text = preg_replace($regexTransport, $replaceTo, $clean_text);
        // Match Miscellaneous Symbols
        $regexMisc = '/[\x{2600}-\x{26FF}]/u';
        $clean_text = preg_replace($regexMisc, $replaceTo, $clean_text);
        // Match Dingbats
        $regexDingbats = '/[\x{2700}-\x{27BF}]/u';
        $clean_text = preg_replace($regexDingbats, $replaceTo, $clean_text);
        return $clean_text;
    }

}